package com.huawei.petSystemdemo.data;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huawei.petSystemdemo.bean.Shop;
import com.huawei.petSystemdemo.bean.User;
import com.huawei.petSystemdemo.bean.Vip;
import com.huawei.petSystemdemo.utils.MyRequest;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

public class UserRepository {

    public static void getUser(int userId, Consumer<User> callback) {
        Map<String, Object> res = new HashMap<>();
        res.put("userId", userId);
        MyRequest.get("/user/getUser", res, json -> {
            JSONObject obj = JSON.parseObject(json);
            boolean success = obj.getBoolean("success");
            String errorMessage = obj.getString("errMessage");
            User user = obj.getObject("data", User.class);
            if (success)
            {
                if (callback != null)
                    callback.accept(user);
            }
            else
            {

            }
        });
    }

    public static void updateIntegral(String username, double money, Consumer<String> callback) {
        Map<String, Object> res = new HashMap<>();
        res.put("username", username);
        res.put("money", money);
        MyRequest.post("/user/updateIntegral", res, new Consumer<String>() {
            @Override
            public void accept(String json) {
                JSONObject obj = JSON.parseObject(json);
                boolean success = obj.getBoolean("success");
                String errorMessage = obj.getString("errMessage");
                if (success)
                {
                    if (callback != null)
                        callback.accept(errorMessage);
                }
                else
                {

                }
            }
        });
    }

    public static void saveVip(int userId, int time, Consumer<String> callback) {
        Map<String, Object> res = new HashMap<>();
        res.put("userId", userId);
        res.put("time", time);

        MyRequest.post("/vip/saveVip", res, new Consumer<String>() {
            @Override
            public void accept(String json) {
                JSONObject obj = JSON.parseObject(json);
                boolean success = obj.getBoolean("success");
                String errorMessage = obj.getString("errMessage");
                if (success)
                {
                    if (callback != null)
                        callback.accept(errorMessage);
                }
                else
                {

                }
            }
        });
    }

    public static void getVip(int userId, Consumer<Vip> callback) {
        Map<String, Object> res = new HashMap<>();
        res.put("userId", userId);

        MyRequest.get("/vip/getVip", res, json -> {

            JSONObject obj = JSON.parseObject(json);
            boolean success = obj.getBoolean("success");
            String errorMessage = obj.getString("errMessage");
            if(success == true) {
                Vip vip = obj.getObject("data", Vip.class);
                if(callback != null) {
                    callback.accept(vip);
                }
//                callback.accept(vip);
            } else {

            }
        });
    }
}
